package com.hs.srm.admin.modules.inforecord.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.common.util.ExcelHelpUtils;
import com.hs.srm.admin.modules.inforecord.dao.InforecordDao;
import com.hs.srm.admin.modules.inforecord.dao.SupplyGoodDao;
import com.hs.srm.admin.modules.inforecord.entity.InforecordEntity;
import com.hs.srm.admin.modules.inforecord.entity.SupplyGoodEntity;
import com.hs.srm.admin.modules.inforecord.service.InforecordService;
import com.hs.srm.admin.modules.material.dao.MaterialDao;
import com.hs.srm.admin.modules.material.dao.MaterialFactoryDao;
import com.hs.srm.admin.modules.material.entity.MaterialEntity;
import com.hs.srm.admin.modules.vendor.dao.VendorDao;
import com.hs.srm.admin.modules.vendor.entity.VendorInfoEntity;
import com.hs.srm.base.common.annotation.DataFilter;
import com.hs.srm.base.common.utils.AddressDataUtil;
import com.hs.srm.base.common.utils.DictDataUtil;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sunnykid.text.SunnykidText;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发票数据Service控制层实现类
 * <p>標題：</p>
 * <p>描述：</p>
 * <p>版權：版權所有 (c) 2019</p>
 * <p>組織：厚声集团</p>
 *
 * @author 鐘曉籟
 * @version V1.0.0
 * @date 2019年4月2日 上午9:46:29
 * @since V1.0.0
 */
@Service("inforecordService")
public class InforecordServiceImpl extends ServiceImpl<InforecordDao, InforecordEntity> implements InforecordService {

    @Autowired
    SupplyGoodDao supplyGoodDao;
    @Autowired
    VendorDao vendorDao;
    @Autowired
    MaterialFactoryDao materialDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<InforecordEntity> page = this.selectsgDetailPage(new QueryParam<InforecordEntity>(params).getPage(),
                new EntityWrapper<InforecordEntity>(), params);
        return new PageUtils(page);
    }

    private Page<InforecordEntity> selectsgDetailPage(Page<InforecordEntity> page, Wrapper<InforecordEntity> wrapper,
                                                      Map<String, Object> params) {
        page.setRecords(this.baseMapper.selectEiPage(page, wrapper, params));
        return page;
    }

    //根据物料号、工厂判断信息记录中是否有该条记录
    @Override
    public boolean checkRecord(String materialCode) {
        boolean result = false;
        List<InforecordEntity> list = this.selectList(new EntityWrapper<InforecordEntity>()
                .like(StringUtils.isNotBlank(materialCode), "material_code", materialCode)
        );
        if (list.size() > 0) {
            result = true;
        }
        return result;
    }

    //根据物料号、工厂判断货源记录中是否有该条记录
    @Override
    public boolean checkRecordFromSupplyGoods(String materialCode, String factory) {
        boolean result = false;
        List<SupplyGoodEntity> list = supplyGoodDao.selectList(new EntityWrapper<SupplyGoodEntity>()
                .like(StringUtils.isNotBlank(materialCode), "material_code", materialCode)
                .like(StringUtils.isNotBlank(factory), "werks", factory)
        );
        if (list.size() > 0) {
            result = true;
        }
        return result;
    }

    //根据物料号、工厂，供应商编码判断货源记录中是否有该条记录
    @Override
    public boolean checkRecordFrominfo(String materialCode, String factory, String srmCode) {
        boolean result = false;
        List<InforecordEntity> list = this.selectList(new EntityWrapper<InforecordEntity>()
                .like(StringUtils.isNotBlank(materialCode), "material_code", materialCode)
                .like(StringUtils.isNotBlank(factory), "werks", factory)
                .like(StringUtils.isNotBlank(srmCode), "srm_code", srmCode)

        );
        if (list.size() > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 导入数据，存于内存并展示给前端页面
     */
    @Override
    public Map<String, Object> importData(File savedFile, String username) {
        Map<String, Object> m = new HashMap<String, Object>();
        List<InforecordEntity> list = ExcelHelpUtils.readExcel(savedFile.getAbsolutePath(), InforecordEntity.class);
        for (InforecordEntity v : list) {
            v.setPkId(SunnykidText.getUUID());
            String vendorCode = v.getSrmCode();
            if (this.vendorDao.findBySrmCode(vendorCode) != null) {
                String vendorName = this.vendorDao.findBySrmCode(vendorCode).getVendorName();
                v.setVendorName(vendorName);
            } else {
                v.setVendorName(" ");
            }

            String materialCode = v.getMaterialCode();
            if (this.materialDao.findByMaterialCode(materialCode) != null) {
                String materialDesc = this.materialDao.findByMaterialCode(materialCode).getMaterialDesc();
                v.setMaterialDesc(materialDesc);
            } else {
                v.setMaterialDesc(" ");
            }
            this.updateById(v);

        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        HttpSession session = request.getSession();
        session.setAttribute("impInforecordList", list);
        return m;
    }

    /**
     * 保存批量导入的数据
     */
    @Override
    public int saveImp() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        HttpSession session = request.getSession();
        List<InforecordEntity> dataList = (List<InforecordEntity>) session.getAttribute("impInforecordList");
        if (dataList == null) {
            throw new RuntimeException("数据已经过期，请重新上传导入！");
        }
        int x = 0;
        for (InforecordEntity ven : dataList) {
            x += this.baseMapper.insert(ven).intValue();
        }
        session.removeAttribute("impInforecordList");// 导入完成后，移除对应的session
        return x;
    }
}
